package com.xvyy.tingshu.search.locktest;

/**
 * ClassName: ThreadStopDoDemo
 * Package: com.xvyy.tingshu.search.locktest
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/6 - 22:33
 * @Version: v1.0
 */
public class ThreadStopDoDemo {

    private static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {

//        testApi1();
//        testApi2();
//        testApi3();
        testApi4();
    }

    private static void testApi4() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!flag) {
                    System.out.println(Thread.currentThread().getName() + "- prepare do some thing");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("被中断了");
                    }
                }
                System.out.println(Thread.currentThread().getName() + "-end");
            }
        });
        thread.setName("A");
        thread.start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        flag = true;
    }

    private static void testApi3() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // interrupted:false
                        Thread.currentThread().interrupt(); // interrupted:ture
                        System.out.println("睡眠期间被中断了");
                    }
                }
                System.out.println(Thread.currentThread().getName() + "-end");
            }
        });
        thread.setName("A");
        thread.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        thread.interrupt();// main线程中断thread线程   修改 interrupted:ture
    }

    private static void testApi2() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!flag) {
                    System.out.println(Thread.currentThread().getName() + "-do some thing");
                }
                System.out.println(Thread.currentThread().getName() + "-end");
            }
        });
        thread.setName("A");
        thread.start();
        Thread.sleep(200);
        flag = true;
    }

    private static void testApi1() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "-do some thing");
                }
                System.out.println(Thread.currentThread().getName() + "-end");
            }
        });
        thread.setName("A");
        thread.start();
        Thread.sleep(200);
        thread.interrupt();// main线程中断thread线程
    }
}
